package org.csu.pet_store_2.persistence;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.csu.pet_store_2.Domain.Account;
import org.springframework.stereotype.Repository;

@Repository
@Mapper
public interface AccountMapper extends BaseMapper<Account> {

    //判断是否存在指定id的account（userid不区分大小写）
    default boolean isExistAccount(String userid) {
        QueryWrapper<Account> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userid", userid);
        return this.exists(queryWrapper);
    }

    //返回指定id的account
    default Account getAccountByUserid(String userid) {
        return this.selectById(userid);
    }

    //插入一个新account，返回实际操作的行数。account不为null且userid必须不在数据库中存在，否则不进行任何修改
    default int setNewAccount(Account account) {
        if (account == null) {
            return 0;
        }
        QueryWrapper<Account> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userid", account.getUserid());
        if (!this.exists(queryWrapper)) {
            return this.insert(account);
        }
        return 0;
    }

    //一次性更新整条记录，返回实际修改的行数。account不能为null且userid必须已经在数据库中存在，否则不进行任何修改
    default int setExistAccount(Account account) {
        if (account != null) {
            return this.updateById(account);
        }
        return 0;
    }

    /*
      单独修改数据库中的某个值，返回修改的行数。userid必须已经在数据库中存在，否则不进行任何修改
     */
    default int setEmailByUserid(String userid, String email) {
        Account account = this.selectById(userid);
        if (account != null) {
            account.setEmail(email);
            return this.updateById(account);
        }
        return 0;
    }
    default int setFirstnameByUserid(String userid, String firstname) {
        Account account = this.selectById(userid);
        if (account != null) {
            account.setFirstname(firstname);
            return this.updateById(account);
        }
        return 0;
    }
    default int setLastnameByUserid(String userid, String lastname) {
        Account account = this.selectById(userid);
        if (account != null) {
            account.setLastname(lastname);
            return this.updateById(account);
        }
        return 0;
    }
    default int setStatusByUserid(String userid, String status) {
        Account account = this.selectById(userid);
        if (account != null) {
            account.setStatus(status);
            return this.updateById(account);
        }
        return 0;
    }
    default int setAddr1ByUserid(String userid, String addr1) {
        Account account = this.selectById(userid);
        if (account != null) {
            account.setAddr1(addr1);
            return this.updateById(account);
        }
        return 0;
    }
    default int setAddr2ByUserid(String userid, String addr2) {
        Account account = this.selectById(userid);
        if (account != null) {
            account.setAddr2(addr2);
            return this.updateById(account);
        }
        return 0;
    }

    default int setCityByUserid(String userid, String city) {
        Account account = this.selectById(userid);
        if (account != null) {
            account.setCity(city);
            return this.updateById(account);
        }
        return 0;
    }
    default int setStateByUserid(String userid, String state) {
        Account account = this.selectById(userid);
        if (account != null) {
            account.setState(state);
            return this.updateById(account);
        }
        return 0;
    }
    default int setZipByUserid(String userid, String zip) {
        Account account = this.selectById(userid);
        if (account != null) {
            account.setZip(zip);
            return this.updateById(account);
        }
        return 0;
    }
    default int setCountryByUserid(String userid, String country) {
        Account account = this.selectById(userid);
        if (account != null) {
            account.setCountry(country);
            return this.updateById(account);
        }
        return 0;
    }
    default int setPhoneByUserid(String userid, String phone) {
        Account account = this.selectById(userid);
        if (account != null) {
            account.setPhone(phone);
            return this.updateById(account);
        }
        return 0;
    }

    //删除指定id的account，返回实际修改的行数
    default int removeAccountByUserid(String userid) {
        return this.deleteById(userid);
    }


    default Account getAccountByPhone(String Phone){
       return null;
    }//需要一个通过手机号返回Account的函数

}

